package cn.online.taxi.common.test;

public class QuickSort {

    public static void quickSort(int[] arr, int low, int high) {
        int i, j ,temp,t;
        if(low > high) {
            return;
        }
        i = low;
        j = high;
        temp = arr[low];
        while(i < j) {
            while(temp <= arr[j] && i < j) {
                j--;
            }
            while(temp >= arr[i] && i < j) {
                i++;
            }
            if(i < j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }
        }
        //最后将基准为与i和j相等位置的数字交换
        arr[low] = arr[i];
        arr[i] = temp;
        quickSort(arr, low, j - 1);
        quickSort(arr, j + 1, high);
    }

    public static String stringToAscii(String value)
    {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(i != chars.length - 1)
            {
                sbu.append((int)chars[i]).append(",");
            }
            else {
                sbu.append((int)chars[i]);
            }
        }
        return sbu.toString();
    }

    public static void main(String[] args) {
        //OVIPWUGMB
        int[] arr = new int[9];
        String str = "OVIPWUGMB";
        String asciiResult = stringToAscii(str);
        String[] as = asciiResult.split(",");
        for(int i = 0; i < as.length; i++) {
            arr[i] = Integer.parseInt(as[i]);
        }
        quickSort(arr, 0, arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print((char)arr[i] + " ");
        }

    }
}
